package com.ds.home.database;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.annotation.JSONField;
import com.ds.common.JDSConstants;
import com.ds.common.logging.Log;
import com.ds.common.logging.LogFactory;
import com.ds.home.*;
import com.ds.home.database.vo.ZNodeVO;
import com.ds.iot.HomeConstants;
import com.ds.iot.ZNode;
import com.ds.iot.enums.DeviceStatus;
import com.ds.iot.enums.ZNodeZType;

import java.util.ArrayList;
import java.util.List;

/**
 * Znode entity.
 *
 * @author MyEclipse Persistence Tools
 */

public class DBZNode implements EIZNode {

    private ZNodeVO vo;


    private static final Log logger = LogFactory.getLog(JDSConstants.CONFIG_KEY, DBZNode.class);

    /**
     * minimal constructor
     */
    protected DBZNode(ZNodeVO vo) {
        this.vo = vo;


    }


    public Integer getChannel() {
        return vo.getChannel();
    }

    public EIZNode addZNode(EIZNode znode) {
        List znodeIds = this.getAllZNodeIds();
        if (!znodeIds.contains(znode.getZnodeid())) {
            znodeIds.add(znode.getZnodeid());
            MEMManagerImpl.getInstance().getZNodeRefZNodeCache().put(this.getZnodeid(), JSONObject.toJSONString(znodeIds));

        }
        return znode;
    }

    public void removeZNode(String znodeId) {
        List znodeIds = this.getAllZNodeIds();
        if (znodeIds.contains(znodeId)) {
            znodeIds.remove(znodeId);
            MEMManagerImpl.getInstance().getZNodeRefZNodeCache().put(this.getZnodeid(), JSONObject.toJSONString(znodeIds));
            // manager.deleteZNodeById( znodeId);
        }
    }

    public List<String> getAllZNodeIds() {
        String idsjson = MEMManagerImpl.getInstance().getZNodeRefZNodeCache().get(this.getZnodeid());
        List znodeIds = new ArrayList<String>();
        if (idsjson == null) {
            List<EIZNode> znodeList = MEMManagerImpl.getInstance().getAllChildZNode(this.getZnodeid());
            for (EIZNode znonde : znodeList) {
                znodeIds.add(znonde.getZnodeid());
            }
            idsjson = JSONObject.toJSONString(znodeIds);
            MEMManagerImpl.getInstance().getZNodeRefZNodeCache().put(this.getZnodeid(), JSONObject.toJSONString(znodeIds));
        }
        znodeIds = JSONArray.parseArray(idsjson, String.class);
        return znodeIds;
    }

    public List<EIZNode> getAllZNodes() {
        List<EIZNode> znodes = new ArrayList<EIZNode>();
        List<String> znodeIds = getAllZNodeIds();
        for (String znodeid : znodeIds) {
            EIZNode znode = MEMManagerImpl.getInstance().getZNodeById(znodeid);
            znodes.add(znode);
        }
        return znodes;
    }

    //
    public String getCreateuiserid() {

        return vo.getCreateuiserid();
    }

    @JSONField(serialize = false)
    public EIDevice getDevice() {

        EIDevice device = MEMManagerImpl.getInstance().getDeviceById(this.getDeviceid());
        if (device == null) {
            logger.error("DBZNode.getDevice() deviceId:[" + this.getDeviceid() + "] not exit!");
        }

        return device;
    }

    public String getDeviceid() {
        return vo.getDeviceid();
    }

    public String getMacaddress() {
        return vo.getMacaddress();
    }

    public String getMasterkey() {
        return vo.getMasterkey();
    }

    public String getName() {
        return vo.getName();
    }

    public String getPanid() {
        return vo.getPanid();
    }

    @JSONField(serialize = false)
    public EIZNode getParentNode() {
        EIZNode znode = MEMManagerImpl.getInstance().getZNodeById(this.getParentid());
        return znode;
    }

    public String getParentid() {
        return vo.getParentid();
    }


    public String getProfilepath() {
        return vo.getProfilepath();
    }

    public Integer getSensortype() {
        return vo.getSensortype();
    }

    public Integer getStatus() {

        String status = vo.getStatus().toString();
        if (status == null) {

            if (this.getZtype() != null && !this.getZtype().equals(ZNodeZType.GATEWAY.getType()) && !this.getZtype().equals(ZNodeZType.SHARE.getType())) {

                if (this.getParentNode() != null && !this.getParentNode().getStatus().equals(DeviceStatus.ONLINE.getCode())) {
                    status = DeviceStatus.OFFLINE.getCode().toString();
                } else {
                    status = vo.getStatus().toString();
                }

            } else {
                status = vo.getStatus().toString();
            }

        }
        return Integer.valueOf(status);
    }

    public String getZmoduleid() {
        return vo.getZmoduleid();
    }

    public String getZnodeid() {
        return vo.getZnodeid();
    }

    public String getZtype() {
        return vo.getZtype();
    }


    public void setChannel(Integer channel) {
        vo.setChannel(channel);

    }

    public void setCreateuiserid(String createuiserid) {
        vo.setCreateuiserid(createuiserid);

    }

    public void setDeviceid(String deviceid) {
        vo.setDeviceid(deviceid);
    }

    public void setMacaddress(String macaddress) {
        vo.setMacaddress(macaddress);
    }

    public void setMasterkey(String masterkey) {
        vo.setMasterkey(masterkey);
    }

    public void setName(String name) {
        vo.setName(name);
    }

    public void setPanid(String panid) {
        vo.setPanid(panid);
    }

    public void setParentid(String parentid) {
        vo.setParentid(parentid);
    }


    public void setProfilepath(String profilepath) {
        vo.setProfilepath(profilepath);
    }

    public void setSensortype(Integer sensortype) {
        vo.setSensortype(sensortype);
    }

    public void setStatus(Integer status) {
        vo.setStatus(status);
    }

    public void setZmoduleid(String zmoduleid) {
        vo.setZmoduleid(zmoduleid);
    }

    public void setZnodeid(String znodeid) {
        vo.setZnodeid(znodeid);
    }

    public void setZtype(String ztype) {
        vo.setZtype(ztype);
    }

    public String getEndPointid() {
        return vo.getEndpointid();
    }

    public void setEndPointid(String endpointid) {
        vo.setEndpointid(endpointid);
    }

    @JSONField(serialize = false)
    public EIDeviceEndPoint getEndPoint() {
        EIDeviceEndPoint endPoint = null;

        if (this.getEndPointid() != null) {
            endPoint = MEMManagerImpl.getInstance().getEndPoindById(this.getEndPointid());
        }
        if (endPoint == null && this.getDevice() != null && this.getDevice().getDeviceEndPoints().size() > 0) {
            endPoint = this.getDevice().getDeviceEndPoints().get(0);
        }

        return endPoint;
    }

    public List<EIAlarm> getAlarmList() {
        List<String> alarmIds = this.getAlarmIds();
        List<EIAlarm> alarms = new ArrayList<EIAlarm>();
        for (String alarmId : alarmIds) {
            alarms.add(MEMManagerImpl.getInstance().getAlarmById(alarmId));
        }
        return alarms;
    }

    public EIAlarm addAlarm(EIAlarm alarm) {
        List alarmIds = this.getAlarmIds();
        if (!alarmIds.contains(alarm.getAlarmid())) {
            alarmIds.add(alarm.getAlarmid());
            MEMManagerImpl.getInstance().getZNodeRefAlarmCache().put(this.getZnodeid(), JSONObject.toJSONString(alarmIds));
        }

        return alarm;
    }

    public void removeAlarm(String alarmId) {
        List alarmIds = this.getAlarmIds();
        if (alarmIds.contains(alarmId)) {
            alarmIds.remove(alarmId);
        }
        MEMManagerImpl.getInstance().getZNodeRefAlarmCache().put(this.getZnodeid(), JSONObject.toJSONString(alarmIds));
    }

    public List<String> getAlarmIds() {
        String idsjson = MEMManagerImpl.getInstance().getZNodeRefAlarmCache().get(this.getZnodeid());
        List alarmIds = new ArrayList<String>();
        if (idsjson == null) {
            List<EIAlarm> alarms = MEMManagerImpl.getInstance().getAlarmsBySensorId(this.getZnodeid());
            for (EIAlarm alarm : alarms) {
                alarmIds.add(alarm.getAlarmid());
            }
            idsjson = JSONObject.toJSONString(alarmIds);
            MEMManagerImpl.getInstance().getZNodeRefAlarmCache().put(this.getZnodeid(), JSONObject.toJSONString(alarmIds));
        }
        alarmIds = JSONArray.parseArray(idsjson, String.class);
        return alarmIds;
    }

    public EIScene addScene(EIScene scene) {
        List sceneIds = this.getSceneIds();
        if (!sceneIds.contains(scene.getSceneid())) {
            sceneIds.add(scene.getSceneid());
            MEMManagerImpl.getInstance().getZNodeRefAlarmCache().put(this.getZnodeid(), JSONObject.toJSONString(sceneIds));
        }

        return scene;
    }

    public List<EIScene> getSceneList() {
        List<String> sceneIds = this.getSceneIds();
        List<EIScene> scenes = new ArrayList<EIScene>();
        for (String sceneId : sceneIds) {
            scenes.add(MEMManagerImpl.getInstance().getSceneById(sceneId));
        }
        return scenes;
    }

    public List<String> getSceneIds() {
        String idsjson = MEMManagerImpl.getInstance().getAllSceneIdCache().get(this.getZnodeid());
        List sceneIds = new ArrayList<String>();
        if (idsjson == null) {
            List<EIScene> scenes = MEMManagerImpl.getInstance().getScenesBySensorId(this.getZnodeid());
            for (EIScene scene : scenes) {
                sceneIds.add(scene.getSceneid());
            }
            idsjson = JSONObject.toJSONString(sceneIds);
            MEMManagerImpl.getInstance().getAllSceneIdCache().put(this.getZnodeid(), JSONObject.toJSONString(sceneIds));
        }
        sceneIds = JSONArray.parseArray(idsjson, String.class);
        return sceneIds;
    }

    public void removeScene(String sceneId) {
        List sceneIds = this.getSceneIds();
        if (sceneIds.contains(sceneId)) {
            sceneIds.remove(sceneId);
            MEMManagerImpl.getInstance().getAllSceneIdCache().put(this.getZnodeid(), JSONObject.toJSONString(sceneIds));
        }
    }

    public EIScene getDefaultScene() {
        EIScene defaultScene = null;
        for (EIScene scene : getSceneList()) {
            if (scene != null && scene.getStatus() == HomeConstants.ZNNODE_ALARM_STATUS_DEFAULT) {
                scene = defaultScene;
                break;
            }
        }
        return defaultScene;
    }

    public boolean equals(Object object) {
        if (this == object) {
            return true;
        }

        if (object != null && object instanceof ZNode) {

            return this.getEndPointid().equals(((ZNode) object).getEndPointid()) && this.getParentid().equals(((ZNode) object).getParentid());
        }

        if (object != null && object instanceof ZNode) {
            return this.getZnodeid().equals(((ZNode) object).getZnodeid());
        } else {
            return false;
        }
    }

    public ZNodeVO getVo() {
        return vo;
    }

    public void setVo(ZNodeVO vo) {
        this.vo = vo;
    }

}